import { request } from "../../utils/util";
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        dataTab: [],
        value: "",
        url: "/search/default",
        suggest: [],
        show: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.storeBindings = createStoreBindings(this, {
            store,
            actions: ['addLick']
        })
        const that = this
        request('/search/hot/detail', 'GET')
            .then(data => {
                console.log(data)
                that.setData({
                    list: data.data
                })
            })
            .catch(err => {
                console.error(err)
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //点击热门搜索
    inputValue(e) {
        // console.log('sdsa', e.currentTarget.dataset.value);
        let value = e.currentTarget.dataset.value;
        this.setData({
            value: value,
            suggest: []
        })
        this.search()
    },
    //点击搜索
    search() {
        const that = this
        let url = that.data.value !== "" ? `/search?keywords=${that.data.value}` : `/search/default`
        request(url, 'GET')
            .then(data => {
                console.log(data)
                if (!data.result.songs) {
                    that.setData({
                        show: 2
                    })
                    return
                }
                that.setData({
                    dataTab: data.result.songs,
                    show: 3
                })
            })
            .catch(err => {
                console.error(err)
            })
    },
    collect(e) {
        console.log('shuhu', e.currentTarget.dataset.value)
        this.addLick(e.currentTarget.dataset.value)
    },
    inputSearch: function (event) {
        console.log('inputSearch', event.detail);
        const that = this
        if (event.detail.length <= 0) {
            that.setData({
                show: 0,
                suggest: []
            })
            return
        }
        request('/search/suggest', 'GET', {
            keywords: event.detail,
            type: 'mobile'
        })
            .then(data => {
                console.log(data.result.allMatch)
                that.setData({
                    suggest: data.result.allMatch,
                    show: 1
                })
            })
            .catch(err => {
                console.error(err)
            })

    }
})